/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.fr.bank;

import com.plian.Tools.base.BaseController;
import com.plian.system.entity.fr.bank.BankAccountInfo;
import com.plian.system.model.CommonCode;
import com.plian.system.model.R;
import com.plian.system.service.fr.bank.IBankAccountInfoService;
import com.plian.system.service.fr.bank.IBankAccountRateRecordService;
import com.plian.system.service.fr.indicatorswarning.IndicatorsWarningService;
import com.plian.system.service.sys.orgzation.OrgzationService;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.wrapper.fr.bank.BankAccountInfoWrapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 控制器
 *
 * @author
 * @since 2020-07-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/bankaccountinfo")
public class BankAccountInfoController extends BaseController {

	private IBankAccountInfoService bankAccountInfoService;

	private OrgzationService orgzationService;

	private IBankAccountRateRecordService bankAccountRateRecordService;

	private IndicatorsWarningService indicatorsWarningService;

	private BankAccountInfoWrapper bankAccountInfoWrapper;

//	/**
//	 * 详情
//	 */
//	@PostMapping("/detail")
//	public R<BankAccountInfoVO> detail(@RequestBody BankAccountInfo bankAccountInfo) {
//		BankAccountInfo detail = bankAccountInfoService.getOne(Condition.getQueryWrapper(bankAccountInfo));
//		R<BankAccountInfoVO> objResult = new R<>(CommonCode.SUCCESS);
//		BankAccountInfoVO bankAccountInfoVO = bankAccountInfoWrapper.entityToVO(detail);
//		objResult.setPageData(bankAccountInfoVO);
//		return objResult;
//	}

	/**
	 * 分页
	 */
//	@PostMapping("/list")
//	public ListResult<BankAccountInfoVO> list(@RequestBody Query query) {
//		List<SearchEntity> entityList = query.getSearch();
//		if (query.getAscs()==null&&query.getDescs()==null){
//			query.setDescs("create_time");
//		}
//		QueryWrapper<BankAccountInfo> queryWrapper = SearchParamEntiy.getQueryWrapper(
//				Condition.getQueryWrapper(new BankAccountInfo()), entityList
//		);
//		if (!Optional.ofNullable(query.getFlag()).isPresent() || query.getFlag() != 1) {
//			queryWrapper.and(wrapper -> wrapper.in("org_id", orgzationService.getSubordinateIds())
//					.or().eq("top_group_id", TokenUtil.getTokenOrgId()));
//		}
//		queryWrapper.apply(" 4  = account_use & 4");
//		IPage<BankAccountInfo> pages = bankAccountInfoService.page(Condition.getPage(query), queryWrapper);
//		List<BankAccountInfoVO> bankAccountInfoVOS = bankAccountInfoWrapper.entityToVO(pages.getRecords());
//		ListResult<BankAccountInfoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(bankAccountInfoVOS);
//		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//		return listResult;
//	}

	/**
	 * 分页
	 */
//	@PostMapping("/addList")
//	public ListResult<BankAccountInfoVO> addList(@RequestBody Query query) {
//		List<SearchEntity> entityList = query.getSearch();
//		if (query.getAscs()==null&&query.getDescs()==null){
//			query.setDescs("create_time");
//		}
//		QueryWrapper<BankAccountInfo> queryWrapper = SearchParamEntiy.getQueryWrapper(
//				Condition.getQueryWrapper(new BankAccountInfo()), entityList
//		);
//		if (!Optional.ofNullable(query.getFlag()).isPresent() || query.getFlag() != 1) {
//			queryWrapper.and(wrapper -> wrapper.in("org_id", orgzationService.getSubordinateIds())
//					.or().eq("top_group_id", TokenUtil.getTokenOrgId()));
//		}
////		queryWrapper.isNull("add_type");
//		IPage<BankAccountInfo> pages = bankAccountInfoService.page(Condition.getPage(query), queryWrapper);
//		List<BankAccountInfoVO> bankAccountInfoVOS = bankAccountInfoWrapper.entityToVO(pages.getRecords());
//		ListResult<BankAccountInfoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(bankAccountInfoVOS);
//		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//		return listResult;
//	}

//	/** TODO DEL
//	 * 新增或修改
//	 */
//	@PostMapping("/submit")
//	@Resubmit(delaySeconds = 0)
//	public R submit(@Valid @RequestBody BankAccountInfo bankAccountInfo) {
//		boolean result =  bankAccountInfoService.saveOrUpdate(bankAccountInfo);
//		if (result){
//			BankAccountRateRecord bankAccountRateRecord = new BankAccountRateRecord();
//			bankAccountRateRecord.setBankAccount(bankAccountInfo.getBankAccount());
//			bankAccountRateRecord.setRate(bankAccountInfo.getRate());
//			bankAccountRateRecordService.saveOrUpdate(bankAccountRateRecord);
//		}
//		return result ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
//
//	}

//	/** TODO DEL
//	 * 删除
//	 */
//	@PostMapping("/remove")
//	public R remove(@RequestBody PageData pd) {
//		List<String> ids = (List<String>)pd.get("ids");
//		return bankAccountInfoService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
//				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
//	}

	/**
	 * 分页
	 */
//	@GetMapping("/refList") TODO DEL
//	public ListResult<BankAccountInfoVO> refList() {
//		List<BankAccountInfo> bankAccountInfos = bankAccountInfoService.list(
//				new LambdaQueryWrapper<BankAccountInfo>().orderByDesc(BankAccountInfo::getCreateTime).and(
//				wrapper -> wrapper.in(BankAccountInfo::getOrgId, orgzationService.getSubordinateIds())
//						.or().eq(BankAccountInfo::getTopGroupId, TokenUtil.getTokenOrgId())
//		));
//		List<BankAccountInfoVO> bankAccountInfoVOS = bankAccountInfoWrapper.entityToVO(bankAccountInfos);
//		ListResult<BankAccountInfoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(bankAccountInfoVOS);
//		listResult.setTotal(CollectionUtil.isNotEmpty(bankAccountInfoVOS) ? bankAccountInfoVOS.size() : 0);
//		return listResult;
//	}

	/**
	 * 账户报表 统计接口
	 * @param bankAccountInfo
	 * @return
	 */
	@PostMapping("/summaryList")
	public R<ReportReturnVO> summaryList(@RequestBody BankAccountInfo bankAccountInfo){
		ReportReturnVO reportReturnVO = bankAccountInfoService.summaryList(bankAccountInfo);
		R<ReportReturnVO> result = new R<>(CommonCode.SUCCESS);
		result.setPageData(reportReturnVO);
		return result;
	}

//	@PostMapping("/summarySumList") TODO DEL
//	public R<ReportReturnVO> summarySumList(@RequestBody BankAccountInfo bankAccountInfo){
//		ReportReturnVO reportReturnVO = bankAccountInfoService.summarySumList(bankAccountInfo);
//		R<ReportReturnVO> result = new R<>(CommonCode.SUCCESS);
//		result.setPageData(reportReturnVO);
//		return result;
//	}

//	/**
//	 * 账户数量超标预警
//	 */
//	@GetMapping("/warn")
//	public ListResult<BankAccountInfoVO> warn() {
//		List<BankAccountInfoVO> bankAccountInfoVOList = bankAccountInfoService.warn();
//		ListResult<BankAccountInfoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(bankAccountInfoVOList);
//		listResult.setTotal(CollectionUtil.isNotEmpty(bankAccountInfoVOList) ? bankAccountInfoVOList.size() : 0);
//		return listResult;
//	}
//
//	/**
//	 * 账户超时预警
//	 */
//	@GetMapping("/timeoutWarning")
//	public ListResult<BankAccountInfoVO> timeoutWarning() {
//		List<BankAccountInfoVO> bankAccountInfoVOList = bankAccountInfoService.timeoutWarning();
//		ListResult<BankAccountInfoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(bankAccountInfoVOList);
//		listResult.setTotal(CollectionUtil.isNotEmpty(bankAccountInfoVOList) ? bankAccountInfoVOList.size() : 0);
//		return listResult;
//	}
//	/**
//	 * 数量超标添加
//	 */
//	@PostMapping("/insertWarning")
//	public ListResult<PageData> insertWarning() {
//		List<BankAccountInfoVO> warn = bankAccountInfoService.warn();
//		ArrayList<PageData> pdlist = new ArrayList<>();
//		for (BankAccountInfoVO vo :warn){
//			String id = UuidUtil.get32UUID();
//			String orgId = vo.getOrgId();
//			//49ade100f5374f2da87c774a7e41e0ae
//			String indicatorsId  = "49ade100f5374f2da87c774a7e41e0ae";
//			Integer WarningType = 3;
//			Integer SourceType = 2;
//			Date createTime = DateUtil.now();
//			String warningTime = DateUtil.format(createTime, "yyyy-MM");
//			int accountNumbers = vo.getAccountNumbers();
//			if (accountNumbers > 1){
//				PageData pd = new PageData();
//				pd.put("id",id);
//				pd.put("orgId",orgId);
//				pd.put("warningTime",warningTime);
//				pd.put("indicatorsId",indicatorsId);
//				pd.put("WarningType",WarningType);
//				pd.put("SourceType",SourceType);
//				pd.put("createTime",createTime);
//				pd.put("warningData",accountNumbers+"");
//				//indicatorsWarningService.insertWarn(pd);
//				pdlist.add(pd);
//			}
//		}
//		ListResult<PageData> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(pdlist);
//		return listResult;
//	}
//
//    /**
//     * 时间超标添加
//     */
//    @PostMapping("/inserttimeWarning")
//    public ListResult<PageData> inserttimeWarning() {
//        List<BankAccountInfoVO> warn = bankAccountInfoService.timeoutWarning();
//        HashMap<String, Integer> orgnum = new HashMap<>();
//        for (BankAccountInfoVO vo :warn){
//            String orgId = vo.getOrgId();
//			boolean b = orgnum.containsKey(orgId);
//			if (b){
//				Integer num = orgnum.get(orgId);
//				orgnum.put(orgId,num+1);
//			}else {
//				orgnum.put(orgId,1);
//			}
//		}
//		Set<String> strings = orgnum.keySet();
//		Iterator<String> iterator = strings.iterator();
//		ArrayList<PageData> pdlist = new ArrayList<>();
//		while (iterator.hasNext()){
//			String orgId = iterator.next();
//			Integer num = orgnum.get(orgId);
//			PageData pd = new PageData();
//			String id = UuidUtil.get32UUID();
//			String indicatorsId  = "7011aa8078ad4ca7a49dd185dbd0fd64";
//			Integer WarningType = 3;
//			Integer SourceType = 2;
//			Date createTime = DateUtil.now();
//			String warningTime = DateUtil.format(createTime, "yyyy-MM");
//			pd.put("id",id);
//			pd.put("orgId",orgId);
//			pd.put("warningTime",warningTime);
//			pd.put("indicatorsId",indicatorsId);
//			pd.put("WarningType",WarningType);
//			pd.put("SourceType",SourceType);
//			pd.put("createTime",createTime);
//			pd.put("warningData",num+"");
//	//		indicatorsWarningService.insertWarn(pd);
//			pdlist.add(pd);
//
//		}
//		ListResult<PageData> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(pdlist);
//        return listResult;
//    }

}
